//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.Expressions
{
    class RangeIntersectionExpression: BinaryRangeExpression
    {
        public RangeIntersectionExpression(Expression left, Expression right)
            : base(left, right)
        {
        }

        public override string GetText(TextContext context)
        {
            return string.Format("{0} {1}", Left.GetText(context), Right.GetText(context));
        }
        internal override Expression Transposed(Cell target)
        {
            return new RangeIntersectionExpression(Left.Transposed(target), Right.Transposed(target));
        }

        protected override ICellEnumerator CreateCellsValue(ICellEnumerator left, ICellEnumerator right)
        {
            if (left is Cell)
                left = ((Cell) left).ToRange();
            if (right is Cell)
                right = ((Cell) right).ToRange();
            if (left is Range && right is Range)
            {
                return Range.Intersection((Range) left, (Range) right);   
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        protected override Expression CreateExpression(CellEnumeratorExpression left, CellEnumeratorExpression right)
        {
            return new RangeIntersectionExpression(left, right);
        }
    }
}